package 力扣刷题.探索初级算法.数组.有效的数独;

import java.util.Arrays;
import java.util.Scanner;

public class Solution {
    public boolean isValidSudoku(char[][] board) {
        boolean rowValid=checkRows(board);
        boolean columnValid=checkColumns(board);
        boolean _3x3Valid=check3x3(board);
        /*System.out.println(rowValid);
        System.out.println(columnValid);
        System.out.println(_3x3Valid);*/
        return rowValid&&columnValid&&_3x3Valid;
    }
    public boolean checkRows(char[][] board) {
        for (int i = 0; i < 9; i++) {
            if(checkDuplicatesInArray(board[i]))
                return false;
        }
        return true;
    }
    public boolean checkColumns(char[][] board) {
        char[][] transposedBoard=transpose(board);
        for (int i = 0; i < 9; i++) {
            if(checkDuplicatesInArray(transposedBoard[i]))
                return false;
        }
        return true;
    }
    public boolean check3x3(char[][] board) {
        char[][] _9arrays=makeAll3x3SquaresInto9Arrays(board);
        for (int i = 0; i < 9; i++) {
            if(checkDuplicatesInArray(_9arrays[i]))
                return false;
        }
        return true;
    }
    public boolean checkDuplicatesInArray(char[] array) {
        int[] charToAsciiArray=new int[9];
        for (int i = 0; i < 9; i++) {
            charToAsciiArray[i]=(int)array[i];
        }
        Arrays.sort(charToAsciiArray);
        for (int i = 0; i < charToAsciiArray.length-1; i++) {
            if(charToAsciiArray[i]!=46&&charToAsciiArray[i]==charToAsciiArray[i+1]) {
                return true;
            }
        }
        return false;
    }

    public char[][] transpose(char[][] board) {
        char[][] transposedBoard=new char[9][9];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                transposedBoard[j][i]=board[i][j];
            }
        }
        return transposedBoard;
    }
    public char[][] makeAll3x3SquaresInto9Arrays(char[][] board) {
        char[][] _9arrays=new char[9][9];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                char[][] _3x3Square=new char[3][3];
                for (int k = 0; k < 3; k++) {
                    for (int l = 0; l < 3; l++) {
                        _3x3Square[k][l] = board[i * 3 + k][j * 3 + l];
                    }
                }
                _9arrays[i*3+j]=make3x3SquaresIntoArray(_3x3Square);
            }

        }
        return _9arrays;
    }
    public char[] make3x3SquaresIntoArray(char[][] _3x3Square) {
        char[] array=new char[9];
        for (int i = 0; i < 3; i++) {
            for (int j = 0; j < 3; j++) {
                array[i*3+j]=_3x3Square[i][j];
            }
        }
        return array;
    }
    public static void main(String[] args) {
        Scanner s=new Scanner(System.in);
        char[][] board=new char[9][9];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                board[i][j]=s.next().charAt(0);
            }
        }

        System.out.println(new Solution().isValidSudoku(board));
    }
}
